package com.kvest.mobile_newspaper.activities;

import com.kvest.mobile_newspaper.R;
import com.kvest.mobile_newspaper.android.ColorPickerDialog;
import com.kvest.mobile_newspaper.core.Settings;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

public class SettingsActivity extends NewspaperBaseActivity
{
	private EditText login;
	private EditText password;
	private SeekBar fontSize;
	
	private int fontColor;
	private int fontBgColor;
	
	private TextView textExample;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        
        textExample = (TextView)findViewById(R.id.text_example_tv);
        
        Settings settings = Settings.getSettings(); 
        
        login = (EditText)findViewById(R.id.login_edt);
        login.setText(settings.getLogin());
        password = (EditText)findViewById(R.id.password_edt);
        password.setText(settings.getPassword());
        fontSize = (SeekBar)findViewById(R.id.font_size);
        fontSize.setProgress(settings.getFontSize());
        fontSize.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
        {
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
			{
				setExampleColors();	
			}
			
			public void onStopTrackingTouch(SeekBar seekBar) {}
			public void onStartTrackingTouch(SeekBar seekBar) {}
		});
        
        fontColor = settings.getFontColor();
        fontBgColor = settings.getFontBgColor();
        setExampleColors();
        
        findViewById(R.id.save_btn).setOnClickListener(new View.OnClickListener() 
        {	
			public void onClick(View v) 
			{
				save();
				
				SettingsActivity.this.finish();
			}
		});
        
        findViewById(R.id.cancel_btn).setOnClickListener(new View.OnClickListener() 
        {	
			public void onClick(View v) 
			{	
				SettingsActivity.this.finish();
			}
		});
        
        
        Button fontColorBtn = (Button) findViewById(R.id.font_color);
        fontColorBtn.setOnClickListener(new View.OnClickListener() 
        {	
			public void onClick(View v) 
			{
				ColorPickerDialog colorPickerDialog = new ColorPickerDialog(SettingsActivity.this);
				colorPickerDialog.setTitle(getResources().getString(R.string.font_color));
				colorPickerDialog.setListener(new ColorPickerDialog.ColorSelectedListener() 
				{	
					public void colorSelected(Integer color) 
					{
						fontColor = color;
						setExampleColors();
					}
				});
				colorPickerDialog.show();	
			}
		});
        
        Button fontBgColorBtn = (Button) findViewById(R.id.font_bg_color);
        fontBgColorBtn.setOnClickListener(new View.OnClickListener() 
        {	
			public void onClick(View v) 
			{
				ColorPickerDialog colorPickerDialog = new ColorPickerDialog(SettingsActivity.this);
				colorPickerDialog.setTitle(getResources().getString(R.string.font_bg_color));
				colorPickerDialog.setListener(new ColorPickerDialog.ColorSelectedListener() 
				{	
					public void colorSelected(Integer color) 
					{
						fontBgColor = color;
						setExampleColors();
					}
				});
				colorPickerDialog.show();
			}
		});
	}
	
	private void setExampleColors()
	{
		textExample.setTextSize(Settings.FONT_SIZE_OFFSET + fontSize.getProgress());
		textExample.setTextColor(fontColor);
		textExample.setBackgroundColor(fontBgColor);
	}
	
	private void save()
	{
		Settings settings = Settings.getSettings();
		settings.setLogin(login.getText().toString());
		settings.setPassword(password.getText().toString());
		settings.setFontSize(fontSize.getProgress());
		settings.setFontColor(fontColor);
        settings.setFontBgColor(fontBgColor);
        
        settings.save();
	}
}
